package com.qingyun.student.controller;


import com.qingyun.common.dto.Result;
import com.qingyun.student.service.IStudentSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学生科目关联表 前端控制器
 * </p>
 *
 * @author your_name
 * @since 2025-01-01
 */
@RestController
public class StudentSubjectController {

    @Autowired
    private IStudentSubjectService studentSubjectService;

    /**
     * 根据学生ID查询科目列表
     * @param studentId 学生ID
     * @return 科目列表
     */
    @GetMapping("/subjects/{studentId}")
    public Result getSubjectsByStudentId(@PathVariable Long studentId,
                                         @RequestHeader("Authorization") String token) {
        List<String> subjects = studentSubjectService.getSubjectsByStudentId(studentId);
        return Result.ok(subjects);
    }

    /**
     * 更新学生的科目信息
     * @param studentId 学生ID
     * @param subjects 科目列表
     * @return 操作结果
     */
    @PostMapping("/update/{studentId}")
    public Result updateStudentSubjects(@PathVariable Long studentId, 
                                       @RequestBody List<String> subjects,
                                       @RequestHeader("Authorization") String token) {
        boolean success = studentSubjectService.updateStudentSubjects(studentId, subjects);
        if (success) {
            return Result.ok("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    /**
     * 删除学生的所有科目关联
     * @param studentId 学生ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{studentId}")
    public Result deleteStudentSubjects(@PathVariable Long studentId,
                                       @RequestHeader("Authorization") String token) {
        boolean success = studentSubjectService.deleteByStudentId(studentId);
        if (success) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}
